/*
 * Runtimer.cpp
 *
 *  Created on: Nov 29, 2012
 *      Author: bouchier
 */
/*! \file Runtimer.cpp
 * \brief The code for the Runtimer class
 */
#include <stdio.h>
#include <string.h>
#include "qetime.h"
#include "Runtimer.h"

//! Runtimer constructor
/*!
 * \param name The timer name, used to identify the code segment when stats are printed
 */
Runtimer::Runtimer(char * name) {
	clear();
	timerName = name;
}

Runtimer::~Runtimer() {}

//! Clear or initialize the counters
void Runtimer::clear()
{
	cumulative = 0;
	count = 0;
	biggest = 0;
	numOver20Ms = 0;
}

//! Start timing a code segment
void Runtimer::start()
{
	startTick = CQETime::ticks();
}

//! Stop timing a code segment & save accumulated time & stats
void Runtimer::stop()
{
	lastMs = CQETime::melapsed(startTick);
	if (lastMs > biggest)
		biggest = lastMs;
	if (lastMs > 20)
		numOver20Ms++;
	cumulative += lastMs;
	count++;
}

//! Print timing stats for code segment
void Runtimer::print()
{
	printf("Runtimer %s avg: %ld, max: %ld, numOver20ms: %d, count: %ld\n",
			timerName, cumulative/count, biggest, numOver20Ms, count);
}
